package com.charlie.meetingmanagement.service.impl;


import com.charlie.meetingmanagement.mapper.RentalMapper;
import com.charlie.meetingmanagement.mapper.RequestMapper;
import com.charlie.meetingmanagement.pojo.po.PageBean;
import com.charlie.meetingmanagement.pojo.po.Request;
import com.charlie.meetingmanagement.pojo.po.Result;
import com.charlie.meetingmanagement.service.RequestService;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class RequestServiceImpl implements RequestService {

    private static final Logger log = LoggerFactory.getLogger(RequestServiceImpl.class);
    @Resource
    private RequestMapper requestMapper;

    @Resource
    private RentalMapper rentalMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize,Integer userId ,Integer rentalId, Integer meetingroomId,
                         LocalDate meetingroomRentaldate, Integer meetingroomRentaltime, Integer approval) {
        Long count = requestMapper.count(userId,rentalId,meetingroomId,meetingroomRentaldate,meetingroomRentaltime,approval);
//        这里使用pageHelper进行分页，非常好用
        PageHelper.startPage(page, pageSize);
        List<Request> requestList = requestMapper.page(userId,rentalId,meetingroomId,meetingroomRentaldate,
                meetingroomRentaltime,approval );

        return new PageBean(count, requestList);
    }

    @Override
    public Result approveRequest(Integer approveId,Integer rentalId, Integer approval) {
        Request request = requestMapper.getAllById(rentalId);
        if (request.getApproval() != 0){
            return Result.error("repeat action");
        }
        try {
            if (approval == 1){
                rentalMapper.insert(request.getMeetingroomId(),request.getMeetingroomRentaldate(),
                        request.getMeetingroomRentaltime(),request.getRentalDescribe(),request.getUserId(),approveId);
                requestMapper.updateApproval(rentalId,approval);
                return Result.success();
            }else if (approval == 2){
                requestMapper.updateApproval(rentalId,approval);
                return Result.success();
            }
            return Result.error("INVALID CHANGE");
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("INVALID INSERT");
        }
    }

    @Override
    public List<Request>  getRentalRequestById(Integer userId) {
        List<Request> request =requestMapper.getRentalRequestById(userId);
        return request;
    }


    @Override
    public void addRequest(Integer userId, Integer meetingroomId, LocalDate meetingroomRentaldate, Integer meetingroomRentaltime, String rentalDescribe) {
        requestMapper.addRequest(userId,meetingroomId,meetingroomRentaldate,meetingroomRentaltime,rentalDescribe,0);
    }


}
